language = "VBScript"
interface = "1.0"

crt.Screen.Synchronous = True

'设备名称
sysname = "F3_AGG"
'设备IP地址
'admin_ip="192.168.99.123"
'admin_user="zmrbak"
'admin_pass="admin"
'snmp_read_pass="admin"
vlan_id = Array(160,120,110,38,39)                 '开启相关的所有 vlan  id
dhcp_select_interface  = Array(120,110,38,39)                     '开启dhcp的 vlan id
distinct_vlan_ip_last = "2"                                '不用开启dhcp 的vlan 的 ip地址最后一位
dhcp_server_dns  =  Array("192.168.100.5")', "192.168.170.2")    'dhcp server dns地址
interface_port_1 = Array("g", "t","160" ,1, 1)               '1、端口类型（g、e）、link类别、关联的vlan、关联端口起止号
interface_port_2 = Array("g", "t","38 39 120" ,2, 2)         '2、端口类型（g、e）、link类别、关联的vlan、关联端口起止号
interface_port_3 = Array("g", "t","38 39 110" ,3, 3)         '3、端口类型（g、e）、link类别、关联的vlan、关联端口起止号
           '4、端口类型（g、e）、link类别、关联的vlan、关联端口起止号
'-----------------------------------------------------把端口参数放入下面的数组中----------------------------------
interface_ports = Array(interface_port_1, interface_port_2, interface_port_3)', interface_port_4, interface_port_5, interface_port_6)


'----------------------------------------- 服务项目是否开启---------------------------------------
DHCP_SERVER_Enabled = 1
'--------------------------------- 积累vlan_id的所有元素到函数sendString中-------------------------
Dim sendString
sendString = ""
vlan_idddd = vlan_id
For i = LBound(vlan_idddd) To UBound(vlan_idddd)
    sendString = sendString & CStr(vlan_idddd(i)) & " "
Next
'-------------------------------积累dhcp_server_dns的所有元素到函数sendABC中----------------------------------
Dim sendABC
sendABC = ""
DDHHCCP = dhcp_server_dns
For k = LBound(DDHHCCP) To UBound(DDHHCCP)
    sendABC = sendABC & CStr(DDHHCCP(k)) & " "
Next
'-------------------------------------- 取出2数组不相同元素------------------------------
Function CompareArrays(arr1, arr2)
    Dim uniqueElements
    Set uniqueElements = CreateObject("Scripting.Dictionary")

    ' 检查arr1中的元素是否不在arr2中
    Dim ii, jj
    For ii = LBound(arr1) To UBound(arr1)
        Dim found
        found = False
        For jj = LBound(arr2) To UBound(arr2)
            If arr1(ii) = arr2(jj) Then
                found = True
                Exit For
            End If
        Next
        If Not found Then
            uniqueElements.Add arr1(ii), True
        End If
    Next

    ' 将唯一元素转换为数组
    Dim resultArray()
    ReDim resultArray(uniqueElements.Count - 1)
    Dim index
    index = 0
    For Each key In uniqueElements.Keys
        resultArray(index) = key
        index = index + 1
    Next

    CompareArrays = resultArray
End Function
Dim uniqueArr
uniqueArr = CompareArrays(vlan_id, dhcp_select_interface )
'-------------------------------------- Set_Host------------------------------
Sub Set_Host
 	crt.Screen.Send "sys"& chr(13)

	crt.Screen.Send "sysname "& sysname & chr(13)          '修改系统名称
	crt.Screen.WaitForString sysname
	crt.Screen.Send "undo info-center enable" & chr(13)     '关闭系统提示
	crt.Screen.WaitForString sysname
	crt.Screen.Send "vlan  batch  "& sendString & chr(13)      '创建vlan
	crt.Screen.WaitForString sysname
	'--------------------------------------
	if DHCP_SERVER_Enabled = 1 then                            'DHCP服务开启
		crt.Screen.Send "dhcp enable" & chr(13)
		crt.Screen.WaitForString sysname
	end if
	'--------------------------------------
End Sub
'---------------------------------------Set_vlanif--------------------------------------------
Sub Set_vlanif
	For s = LBound(dhcp_select_interface) To UBound(dhcp_select_interface)
		crt.Screen.Send "interface vlanif  "& dhcp_select_interface(s) & chr(13)
		crt.Screen.WaitForString dhcp_select_interface(s)
		crt.Screen.Send "ip add 192.168."& dhcp_select_interface(s)  & ".1 24" & chr(13)
		crt.Screen.WaitForString dhcp_select_interface(s)
		crt.Screen.Send "dhcp select interface " & chr(13)
		crt.Screen.WaitForString dhcp_select_interface(s)
		crt.Screen.Send "dhcp server dns-list  " & sendABC & chr(13)
		crt.Screen.WaitForString dhcp_select_interface(s)
		crt.Screen.Send "quit" & chr(13)
		crt.Screen.WaitForString sysname
	Next
	For ss = LBound(uniqueArr) To UBound(uniqueArr)
		crt.Screen.Send "interface vlanif  "& uniqueArr(ss) & chr(13)
		crt.Screen.WaitForString uniqueArr(ss)
		crt.Screen.Send "ip add 192.168."& uniqueArr(ss) &"." & distinct_vlan_ip_last & "  24" & chr(13)
		crt.Screen.WaitForString uniqueArr(ss)
		crt.Screen.Send "quit" & chr(13)
		crt.Screen.WaitForString sysname
	Next
End Sub
'----------------------------------------------------------------------------------

'------------------------------------------------------------------------------------------------------------------
Function my_interface_port(arr)
    nettype = arr(0)
	link_type = arr(1)
	vports = arr(2)
	start_port = arr(3)
	end_port = arr(4)
	ports=start_port
	Do
		if ports<=end_port then
			crt.Screen.Send "interface "&nettype&"0/0/"&ports & chr(13)
			crt.Screen.WaitForString sysname
              If link_type = "a" Then
                crt.Screen.Send "port link-type access" & chr(13)
		        crt.Screen.WaitForString sysname
	            crt.Screen.Send "port default vlan "& vports & chr(13)
		        crt.Screen.WaitForString sysname
              Elseif link_type = "t" Then
				crt.Screen.Send "port link-type trunk" & chr(13)
		    	crt.Screen.WaitForString sysname
	        	crt.Screen.Send "port trunk allow-pass vlan  "& vports  & chr(13)
	        	crt.Screen.WaitForString sysname
			  end if
			ports=ports+1
		else
			crt.Screen.Send "q"& chr(13)
			exit do
		end if
	Loop
End Function

'---------------------------------------------------------------------------------------
Sub Main
	Call Set_Host
 	Call Set_vlanif
	For j = LBound(interface_ports) To UBound(interface_ports)
    	Call my_interface_port(interface_ports(j))
	Next

End Sub


